package edu.whu.xterm.activity;

import android.support.v4.app.FragmentActivity;
import android.view.View;
import edu.whu.xterm.R;
import edu.whu.xterm.XApplication;

public abstract class LoadingActivity extends FragmentActivity {

	View loadingView;

	View failedView;

	private View target;

	public void initLoadingView(final View target) {
		this.target = target;
		loadingView = findViewById(R.id.loading_widget);
		failedView = findViewById(R.id.loading_failed_widget);

		failedView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loading();
				executeLoadTask();
			}
		});
	}

	protected XApplication getXApplication() {
		return (XApplication) getApplication();
	}

	public void loading() {
		loadingView.setVisibility(View.VISIBLE);
		if (target != null)
			target.setVisibility(View.GONE);
		failedView.setVisibility(View.VISIBLE);
	}

	public void loadFailed() {
		loadingView.setVisibility(View.GONE);
		if (target != null)
			target.setVisibility(View.GONE);
		failedView.setVisibility(View.VISIBLE);
	}

	public void loadSuccessed() {
		loadingView.setVisibility(View.GONE);
		if (target != null)
			target.setVisibility(View.VISIBLE);
		failedView.setVisibility(View.GONE);
	}

	public void afterLoading(boolean success) {
		loadingView.setVisibility(View.GONE);
		if (target != null) {
			target.setVisibility(success ? View.VISIBLE : View.GONE);
		}
		failedView.setVisibility(success ? View.GONE : View.VISIBLE);
	}
	
	public abstract void executeLoadTask();

}
